#! /usr/bin/env racket
#lang racket

;; write as s-expression
;; 5+4+(2-(3-(6+4/5)))
;; -------------------
;;    3(6-2)(2-7)
(/ (+ 5 4 (- 2 (- 3 (+ 6 (/ 4 5)))))
   (* 3 (- 6 2) (- 2 7)))

;; define func to return sum of bigger 2 of 3 numbers
(define (sum-of-big2 a b c)
  (cond
    [(and (<= a b) (<= a c)) (+ b c)]
    [(and (<= b a) (<= b c)) (+ a c)]
    (else (+ a b))))
;; test
(display "(sum-of-big2 10 13 22) => 35\n")
(sum-of-big2 10 13 22)
(display "(sum-of-big2 -1 -2 5) => 4\n")
(sum-of-big2 -1 -2 5)

;; cube root , improve is (x/y*y + 2y)/3
(define (cube x) (* x x x))
(define (abs x) (if (> x 0) x (- x)))
(define (cube-root x)
  (define (cube-root-iter guess)
    (if (good-enough? guess)
      guess
      (begin
        ;; (display (improve guess))
        ;; (display "\n")
        (cube-root-iter (improve guess))
      )))
  (define (good-enough? guess)
    (< (abs (- (cube guess) x)) 0.00001))
  (define (improve guess)
    (/ (+ (/ x (* guess guess))
          (* 2 guess))
       3))
  (cube-root-iter 1.0))
;; test
(cube-root 20)
(cube-root 1000)
